/*
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-08-20 07:02:46
 * @LastEditors: GGB
 * @LastEditTime: 2021-08-20 21:20:51
 */
import 'package:flutter/material.dart';
import 'package:flutter_data_center/components/custom_calendar.dart';
import 'package:flutter_data_center/controller/teacher_attendance_controller.dart';
import 'package:get/get.dart';
import 'package:table_calendar/table_calendar.dart';

import './components/teacher_count_card.dart';
import './components/teacher_list.dart';

class TeacherAttendanceScreen extends StatefulWidget {
  const TeacherAttendanceScreen({Key key}) : super(key: key);
  static String routeName = "/teacher_attendance";

  @override
  _TeacherAttendanceScreenState createState() =>
      _TeacherAttendanceScreenState();
}

class _TeacherAttendanceScreenState extends State<TeacherAttendanceScreen> {
  TeacherAttendanceController _attendanceController =
      Get.put(TeacherAttendanceController());
  final keyWidget = GlobalKey();
  CalendarFormat _calendarFormat = CalendarFormat.week;
  double height = 0;
  DateTime _selectedDay;
  DateTime _focusedDay;

  void calculateSizeAndPosition() =>
      WidgetsBinding.instance.addPostFrameCallback((_) {
        final RenderBox box = keyWidget.currentContext.findRenderObject();
        setState(() {
          height = box.size.height + 90;
        });
      });

  @override
  void initState() {
    super.initState();
    calculateSizeAndPosition();
    // 发起网络请求
    _attendanceController.getTeachersStatistics();
    // _alertController.getAlertStatistics();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: buildAppBar(),
        body: SafeArea(
          child: SingleChildScrollView(
            physics: BouncingScrollPhysics(),
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 20),
              child: Column(
                children: [
                  Column(
                    key: keyWidget,
                    children: [
                      CustomCalendar(
                        focusedDay: _focusedDay,
                        selectedDay: _selectedDay,
                        calendarFormat: _calendarFormat,
                        onDaySelected: (selectedDay, focusedDay) {
                          setState(() {
                            _selectedDay = selectedDay;
                            _focusedDay =
                                focusedDay; // update `_focusedDay` here as well
                          });
                        },
                        onFormatChanged: (format) {
                          print(format);
                          if (_calendarFormat != format) {
                            setState(() {
                              _calendarFormat = format;
                            });
                          }
                        },
                      ),
                      SizedBox(height: 10),
                      TeacherCountCard(),
                    ],
                  ),
                  Container(
                    height: MediaQuery.of(context).size.height - height,
                    child: Padding(
                      padding: const EdgeInsets.only(top: 10),
                      child: TeacherList(),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      );
  AppBar buildAppBar() {
    return AppBar(
      centerTitle: true,
      backgroundColor: Colors.transparent,
      title: Text(
        "教师考勤",
        style: TextStyle(
          fontSize: 18,
        ),
      ),
      elevation: 0,
    );
  }
}
